package br.unifor.lec.aco.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;

import br.unifor.lec.aco.Parameters;
import br.unifor.lec.aco.graph.AcoLink;
import br.unifor.lec.aco.graph.Grafo;
import br.unifor.lec.aco.graph.Vertice;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseMultigraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class VisualizacaoJUNG {

	
	public void visualizar(Grafo g){
		Graph<Vertice, AcoLink> sgv = new SparseMultigraph<Vertice, AcoLink>();
		// Add some vertices. From above we defined these to be type Integer.

		for(int i=0; i<g.getNumVertices();i++){
			Vertice verTemp =g.getVertices()[i];
			sgv.addVertex(verTemp);
			for(int j=0; j<verTemp.adjacents.size();j++){
				if(verTemp.adjacents.get(j)>i){
					sgv.addEdge(new AcoLink(i,1), verTemp, 
							g.getVertices()[verTemp.adjacents.get(j)],EdgeType.UNDIRECTED);
				}
			}
			
		}
		
		//sgv.addEdge(new AcoLink(1, 1), n1,n2,EdgeType.UNDIRECTED); // Note that Java 1.5 auto-boxes primitives
		
		
		Transformer<Vertice, Paint> vertexPaint = new Transformer<Vertice,Paint>() {
			public Paint transform(Vertice i) {
				if(i.gateway)               //colocar aki IF gateway ou target cor diferente
					return Color.GREEN;
				else if(i.target)
					return Color.RED;
			return Color.YELLOW;
			}
		};

		// The Layout<V, E> is parameterized by the vertex and edge types

		StaticLayout<Vertice, AcoLink> layout = new StaticLayout<Vertice, AcoLink>(sgv);
		
		Parameters p = new Parameters();
		int tmpi=0;
		//distance between the nodes
	    int distX=50;
	    int distY=50;
		 for (int i=0;i<p.ladoGrid;i++) {
		        for (int j=0;j<p.ladoGrid;j++) {
		            layout.setLocation(g.getVertices()[tmpi++], i*distX+30, j*distY+30);
		        }
		    }     
		
		layout.setSize(new Dimension(3500,500)); // sets the initial size of the space
		BasicVisualizationServer<Vertice,AcoLink> vv =
			new BasicVisualizationServer<Vertice,AcoLink>(layout);
		vv.setPreferredSize(new Dimension(1000,1000)); //Sets the viewing area size
		
		vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
		//vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller());

		JFrame frame = new JFrame("Simple Graph View");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(vv);
		frame.pack();
		frame.setVisible(true);
	}
}
